//
//  NBAScheduleDept.swift
//  NBA
//
//  Created by tiger on 2018/2/23.
//  Copyright © 2018年 Meigabay. All rights reserved.
//

import UIKit

/// 比赛管理部门
public class NBAGameDept: NBADepartment { 
    
    /// 创建一场比赛
    ///
    /// - Parameters:
    ///   - home: 主队
    ///   - visitors: 客队
    /// - Returns: 比赛
    /// - Throws: 错误异常
    public func createPracticeGame() throws -> NBAGame {
        let game = NBAGame()
        game.home = NBAUser.shared.club
        game.visitors = NBA.shared.clubManagerDept.otherClubs()[0]
        game.time = NSDate().timeIntervalSince1970
        game.place = game.home.venue
        game.brainCenter = NBAGameBrainCenter()
        do {
            try game.referee = NBA.shared.refereeDept.sendReferee(game: game)
            return game
        } catch {
            throw(error)
        }
    }
    
    /// 创建一场比赛
    ///
    /// - Parameters:
    ///   - home: 主队
    ///   - visitors: 客队
    /// - Returns: 比赛
    /// - Throws: 错误异常
    public func createGame(home: NBAClub,visitors: NBAClub) throws -> NBAGame {
        let game = NBAGame()
        game.home = home
        game.visitors = visitors
        game.time = NSDate().timeIntervalSince1970
        game.place = home.venue
        do {
          try game.referee = NBA.shared.refereeDept.sendReferee(game: game)
            return game
        } catch {
            throw(error)
        }
    }
}

public protocol NBAGameProtocol: NSObjectProtocol {
    func playByPlayDidUpdated()
}

public enum NBAGameState: UInt {
    case notstart  //还未开赛
    case jump  //还未开赛
    case playing   //正在比赛
    case pause     //暂停中
    case end       //已经结束
}


/// 一场篮球赛
public class NBAGame {
    
    public weak var delegate: NBAGameProtocol?
    
    public var state: NBAGameState!
    //主队
    public var home: NBAClub!
    //客队
    public var visitors: NBAClub!
    //时间
    public var time: TimeInterval! //时间
    //地点
    public var place: String! //地点
    //裁判
    public var referee: NBAReferee!
    //主队分值
    public var homeScore: Int!
    //客队分值
    public var visitorsScore: Int!
    //结果
    
    //文字直播内容
    public lazy var fullPlayByPlay = [NBAGamePlayByPlayText]()
    
    
    private var gameTime: TimeInterval!
    internal var brainCenter: NBAGameBrainCenter!
    
    internal init() {
        self.state = .notstart
    }
}

/// 游戏思考中心
public class NBAGameBrainCenter {
    
    weak var game: NBAGame?
    private lazy var thinkQueue: DispatchQueue = DispatchQueue(label: "GameBrain")
    
    func startGame(game: NBAGame) {
        self.game = game
        self.thinkQueue.sync {
            self.thinking()
        }
    }
    
    //每隔0.1秒 做一次帧验证
    func thinking() {
        
        //我是谁  //我是比赛计算中心
        //我要到哪里去 //你哪里都不能去
        //我是不是要投篮 //你不能投篮
    }
}

public extension NBAGame {
    
    public func start() {
        self.homeScore = 0
        self.visitorsScore = 0
        self.home.startGame(game: self)
        self.visitors.startGame(game: self)
        self.brainCenter.startGame(game: self)
        self.referee.startGame(game: self)
//        let text = NBAGamePlayByPlayText()
//        text.time = Date()
//        text.isHomeTeam = true
//        text.playText = "颜射三分"
//        text.score = "3-0"
//        self.fullPlayByPlay.append(text)
//        Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(update), userInfo: nil, repeats: false)
    }
    
    @objc func update() {
        let text = NBAGamePlayByPlayText()
        text.time = Date()
        text.isHomeTeam = false
        text.playText = "颜射三分"
        text.score = "3-3"
        self.fullPlayByPlay.append(text)
        self.delegate?.playByPlayDidUpdated()
    }
}

public class NBAGamePlayByPlayText {
    public var time: Date!
    public var isHomeTeam: Bool!
    public var playText: String!
    public var score: String!
}
